package org.nico.cb.section;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.nico.aoc.aspect.AspectBuddy;
import org.nico.aoc.aspect.AspectProxy;
import org.nico.aoc.aspect.point.AspectPoint;
import org.nico.aoc.aspect.point.ProcessingAspectPoint;
import org.nico.aoc.scan.annotations.After;
import org.nico.aoc.scan.annotations.Around;
import org.nico.aoc.scan.annotations.Before;
import org.nico.aoc.scan.annotations.Label;
import org.nico.aoc.scan.annotations.Wrong;
import org.nico.db.datasource.TransationManager;

/** 
 * 
 * @author nico
 */
//@Aspect	//这里不使用注解，使用XML配置增加清晰明了
public class TransationSection implements AspectProxy{

	@Label
	private TransationManager tm;
	
	@Override
	@Before(value = "expression(org.nico.ct.service.impl.*.*(..))")
	public void before(AspectPoint point) {
		try {
			tm.beginTransaction();  
		} catch (SQLException e) {
			e.printStackTrace();
		} 
	}
	
	@Override
	@Around(value = "expression(org.nico.ct.service.impl.*.*(..))")
	public Object around(ProcessingAspectPoint point) throws Throwable {
//		long start = System.currentTimeMillis();
		Object result = point.process();
//		System.out.println("执行操作" + AspectBuddy.getTargetObject(point.getObj()).getClass().getName() + "." + point.getMethod().getName() + "共花费时间：" + (System.currentTimeMillis() - start) + "（ms）");
		return result;
	}

	@Override
	@After(value = "expression(org.nico.ct.service.impl.*.*(..))")
	public void after(AspectPoint point) {
		try {
			tm.commitTransaction();
			tm.relaseConnection();
		} catch (SQLException e) {
		} 
	}

	@Override
	@Wrong(value = "expression(org.nico.ct.service.impl.*.*(..))")
	public void wrong(ProcessingAspectPoint point, Throwable e) throws Throwable{
		tm.rollbackTransaction();
		throw e;
	}


}
